using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace AHWrapper{

	public class AHVisuals : Form{
		//------------------------------------CLASS INSTANCE VARIABLES-----------------------------------------//
		//Buttons for player number selection
		private Button[] playerSelB;
		
		//Map movement buttons (adjacent locations)
		private Button[] surLocB;
		//Map movement button (remain at current location)
		private Button stay;
	
		//STAT SLIDER MENU VARIABLES//
		//Stat trackbars
		private TrackBar[] statBars;
		//Submit stat changes
		private Button statSubmit;
		//---------------------------//
	
		//Buttons for combat
		private Button sneak;
		private Button fight;
	
		//The main game instance
		public Game ahGame;
		//------------------------------------END CLASS INSTANCE VARIABLES------------------------------------//
		

		//Initial Menu Screen/default class constructor
		public AHVisuals (){
			Text = "Arkham Horror";//Title of winform
			ClientSize = new Size(500,500);//Window size
			
			//--------------Buttons to control investigator number----------------------//
			playerSelB =new Button[6];
			for(int i=0;i<6;i++){
				playerSelB[i] = new Button();
				playerSelB[i].Click += new EventHandler(playerSelClick);
				playerSelB[i].Text=(i+1).ToString();
				playerSelB[i].Location = new Point(100,50+i*50);
				playerSelB[i].BackgroundImage = Image.FromFile("img/button.bmp");
				Controls.Add(playerSelB[i]);
				playerSelB[i].Tag = i;
			}
		}
		
		public void drawMain(){
		//draw window for current phase
			switch(ahGame.phase){
				//initial stat selection phase
				case PhaseType.Upkeep1:
					upkeep1();
					break;
				//initial mythos phase	
				case PhaseType.Myth1:
					myth();
					break;
				//upkeep phase	
				case PhaseType.Upkeep:
					upkeep();
					break;
				//movement phase	
				case PhaseType.Move:
					move();
					break;
				//Arkham Encounter Phase	
				case PhaseType.AEnc:
					aEnc();
					break;
				//Gate Encounter Phase	
				case PhaseType.GEnc:
					gEnc();
					break;
				//Mythos Phase	
				case PhaseType.Myth:
					myth();
					break;
				default:
					break;
			}
		}
		public void upkeep1(){
			statSelectorMenu();
		}
		public void upkeep(){
			ahGame.upkeep();
			statSelectorMenu();
		}
		public void move(){
			if(ahGame.moveStart()){
				locationMenu();
			}else{
				drawMain();
			}
		}
		
		public void locationMenu(){
		//draws the location menu		
			//Adjacent Location buttons
			surLocB = new Button[8];
			for(int i=0;i<8;i++){
				surLocB[i] = new Button();
				surLocB[i].Click += new EventHandler(surLocBClick);
				surLocB[i].Tag = i;
			}
			surLocB[Constants.North].Location=new Point(150,20);
			surLocB[Constants.Northeast].Location=new Point(250,20);	
			surLocB[Constants.East].Location=new Point(250,70);
			surLocB[Constants.Southeast].Location=new Point(250,120);
			surLocB[Constants.South].Location=new Point(150,120);
			surLocB[Constants.Southwest].Location=new Point(50,120);
			surLocB[Constants.West].Location=new Point(50,70);
			surLocB[Constants.Northwest].Location=new Point(50,20);
			int count = 0;
			Location curPos=ahGame.getCurLoc();
			foreach(Button b in surLocB){
				if(curPos.adjacent[count]!=null){
					Controls.Add(b);
					b.Text=curPos.adjacent[count].name;
				}
				count++;
			}
			//Button to remain in same location
			stay = new Button();
			stay.Location=new Point(150,70);
			stay.Text="Stay";
			stay.Click += new EventHandler(b_stayClick);
			Controls.Add(stay);
		}		
		public void surLocBClick(object sender, EventArgs e){
		//Action clicking on a surrounding location to move
			for(int i=0;i<8;i++){
				surLocB[i].Dispose();
				stay.Dispose();
			}
			if(ahGame.encTest(true)>0){
				mEncMenu((int)((Button)sender).Tag);
			}
			else{
				ahGame.move((int)((Button)sender).Tag);
				drawMain();
			}
		}
		public void b_stayClick(object sender, EventArgs e){
		//Action for clicking on the say button to remain in location
			ahGame.stopMoves();
			for(int i=0;i<8;i++){
				surLocB[i].Dispose();
				stay.Dispose();
			}
			if(ahGame.encTest(true)>0){
				mEncMenu(0);
			}
			else{
				ahGame.end();
			}
			drawMain(); 
		}
		public void statSelectorMenu(){
		//draws the statistic selector for the upkeep phase
			Investigator curPlayer=ahGame.getCurPlayer();
			statBars = new TrackBar[3];
			for(int i=0; i<3; i++){
				statBars[i] = new TrackBar();
				statBars[i].Location = new Point(10,10+i*150);
				statBars[i].Size = new Size(250,50);
				statBars[i].SmallChange=1;
				statBars[i].LargeChange=1;
				statBars[i].Minimum = curPlayer.stats[1+2*i,Constants.Min];
				statBars[i].Maximum = curPlayer.stats[1+2*i,Constants.Max];
				statBars[i].Value = curPlayer.stats[1+2*i,Constants.Cur];
				Controls.Add(statBars[i]);
			}
			
			statSubmit = new Button();
			statSubmit.Text = "Submit";
			statSubmit.Location = new Point(50,330);
			statSubmit.Click += new EventHandler(submitStats);
			Controls.Add(statSubmit);
		}
		public void submitStats(object sender, EventArgs e){
			ahGame.setStats(statBars[0].Value,statBars[1].Value,statBars[2].Value);
			for(int i=0; i<3; i++){
				statBars[i].Dispose();
			}
			statSubmit.Dispose();
			drawMain();
		}
		public void playerSelClick(object sender, EventArgs e){
			//create game instance for 1 investigator
			ahGame = new Game((int)((Button)sender).Tag+1);
			for(int i=0;i<6;i++){
				playerSelB[i].Dispose();
			}
		
			//draw main screen
			drawMain();
		}
		public void myth(){
			ahGame.myth(); 
			drawMain();
		}	
		public void mEncMenu(int pDirection){
			sneak = new Button();
			fight = new Button();
			sneak.Tag=pDirection;
			sneak.Location = new Point(100,150);
			fight.Location = new Point(100,250);
			sneak.Click += new EventHandler(mSneak);
			fight.Click += new EventHandler(mFight);
			Controls.Add(sneak);
			Controls.Add(fight);		
		}
		public void mSneak(Object sender, EventArgs e){
			sneak.Dispose();
			fight.Dispose();
			int menu=ahGame.mEncSneak();
			if(menu==Constants.MenuLoc){
				ahGame.move((int)((Button)sender).Tag);
				drawMain();
			}else if(menu==Constants.MenuEnc){
				mEncMenu((int)((Button)sender).Tag);
			}else{
				drawMain();
			}
		}
		public void mFight(Object sender, EventArgs e){
			sneak.Dispose();
			fight.Dispose();
			int menu=ahGame.mEncFight();
			if(menu==Constants.MenuEnc){
				mEncMenu(0);
			}else{
				drawMain();
			}
		}
		public void aEnc(){
			ahGame.end();
		}
		public void gEnc(){
			ahGame.end();
		}
	}
}





